package com.admin4j.server.system.controller;

import com.admin4j.common.util.UserContextUtil;
import com.admin4j.framework.security.annotation.AnonymousAccess;
import com.admin4j.framework.web.pojo.R;
import com.admin4j.server.system.entity.vo.MenuVO;
import com.admin4j.server.system.entity.vo.SysUserLoginInfoVO;
import com.admin4j.server.system.manager.IUserProfileService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author andanyang
 * @since 2023/11/14 14:29
 */
@RestController
@RequestMapping("profile")
public class UserProfileController {

    @Autowired
    IUserProfileService userProfileService;

    @GetMapping("menus")
    @ApiModelProperty("我的菜单")
    public R<List<MenuVO>> menus() {

        List<MenuVO> menuVOS = userProfileService.menus(UserContextUtil.getUserId());
        return R.ok(menuVOS);
    }

    @GetMapping("menuTree")
    @ApiModelProperty("我的菜单")
    public R<List<MenuVO>> menuTree() {

        List<MenuVO> menuVOS = userProfileService.menus(null);
        return R.ok(menuVOS);
    }

    @GetMapping()
    @ApiModelProperty("用户详情")
    public R<SysUserLoginInfoVO> userInfo() {

        Long userId = UserContextUtil.getUserId();
        SysUserLoginInfoVO sysUserLoginInfoVO = userProfileService.getUserInfoByUserId(userId);

        return R.ok(sysUserLoginInfoVO);
    }

    @GetMapping("permission")
    @ApiModelProperty("用户权限")
    public R<List<String>> permissionCodes() {

        Long userId = UserContextUtil.getUserId();
        List<String> permissionCodes = userProfileService.permissionCodes(userId);

        return R.ok(permissionCodes);
    }


    @GetMapping("1")
    @ApiModelProperty("注解式匿名url访问")
    @AnonymousAccess
    public R<SysUserLoginInfoVO> get() {

        Long userId = (long) (1 / 0);
        SysUserLoginInfoVO sysUserLoginInfoVO = userProfileService.getUserInfoByUserId(userId);

        return R.ok(sysUserLoginInfoVO);
    }

    @GetMapping("2")
    @ApiModelProperty("yml 忽略")
    public R<SysUserLoginInfoVO> get2() {

        Long userId = 1L;
        SysUserLoginInfoVO sysUserLoginInfoVO = userProfileService.getUserInfoByUserId(userId);

        return R.ok(sysUserLoginInfoVO);
    }

    @GetMapping("3")
    @ApiModelProperty("yml 忽略")
    public R<String> get3() {


        return R.ok("3");
    }

    @GetMapping("4")
    @ApiModelProperty("用户详情")
    @PreAuthorize("hasAuthority('profile')")
    public R<SysUserLoginInfoVO> get4() {

        Long userId = 1L;
        SysUserLoginInfoVO sysUserLoginInfoVO = userProfileService.getUserInfoByUserId(userId);
// AccessDeniedException
        return R.ok(sysUserLoginInfoVO);
    }

    @GetMapping("5")
    @ApiModelProperty("用户详情5")
    @PreAuthorize("hasAuthority('menus')")
    public R<String> get5() {

// AccessDeniedException
        return R.ok("5");
    }
}
